package com.lyl.pipeline.core;

import com.lyl.pipeline.annotation.Pipeline;
import com.lyl.pipeline.handler.BasePipeNodeHandler;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author Mr.Luo
 * @date 2022-07-26 15:36
 * @desc 处理流水线
 */
@Slf4j
@Aspect
@Component
public class PipelineAop {

    @Resource
    private Map<String, BasePipeNodeHandler> handlerMap;


    @Pointcut("@annotation(pipeline)")
    public void pointcut(Pipeline pipeline) {
    }

    @Around("pointcut(pipeline)")
    public Object around(ProceedingJoinPoint pjp, Pipeline pipeline) throws Throwable {
        Object returnObj = PipelineContext.builder(pjp, pipeline, handlerMap).build().execute();
        return Objects.nonNull(returnObj) ? returnObj : pjp.proceed(pjp.getArgs());
    }
}
